package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.AddSign;
import com.yurun.commonapi.pojo.Device;
import com.yurun.commonapi.pojo.Sign;
import com.yurun.commonapi.pojo.StudentDocument;
import com.yurun.commonapi.vo.DeviceListVO;
import com.yurun.commonapi.vo.PersonVO;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgstudentmodule.service.FaceService;
import com.yurun.orgstudentmodule.service.Impl.FaceServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/signManage")
public class FaceController {
//    private static org.apache.log4j.Logger logger = Logger.getLogger(FaceController.class);
    @Autowired
    private FaceService faceService;

    @ResponseBody
    @PostMapping("/setIdentifyCallback")
    public CommonResult setIdentifyCallback(@RequestBody PersonVO personVO) {
        return faceService.setIdentifyCallback(personVO);
    }
    public Date transferString2Date(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
        } catch (ParseException e) {
            //LOGGER.error("时间转换错误, string = {}", s, e);
        }
        return date;
    }


    @ApiOperation(value = "补登打卡")
    @PostMapping("/addSign")
    public CommonResult addSign(ServletRequest request, AddSign addSign) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
//        String organizationId = JWTUtil.getOrganizationId(token);
//        studentDocument.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        Sign sign = new Sign();
        sign.setStudentId(addSign.getStudentId());
        System.out.println("444444444444444444");
        sign.setSignDate(transferString2Date(addSign.getSignDate()));

//        System.out.println(transferString2Date(addSign.getSignDate()));
//        System.out.println(addSign);
//        System.out.println(sign);
        sign.setSignFist(transferString2Date(addSign.getSignFist()));
//        System.out.println(transferString2Date(addSign.getSignFist()));
//        System.out.println(addSign);
//        System.out.println(sign);
        sign.setSignLast(transferString2Date(addSign.getSignLast()));
        System.out.println("1111111111111111");
        sign.setSignTemperature(addSign.getSignTemperature());
        sign.setCreateTime(new Date());
        System.out.println(addSign);
        System.out.println(sign);
        return faceService.addSign(sign);
    }

    @ResponseBody
    @PostMapping("/addDevice")
    public CommonResult addDevice(@RequestBody PersonVO personVO, ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        personVO.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return faceService.addDevice(personVO);
    }

    @ResponseBody
    @PostMapping("/getDeviceList")
    public CommonResult getDeviceList(@RequestBody DeviceListVO deviceListVO, ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        deviceListVO.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return faceService.getDeviceList(deviceListVO);
    }

    @ResponseBody
    @PostMapping("/deleteDevice")
    public CommonResult deleteDevice(@RequestBody Device device) {
        return faceService.deleteDevice(device);
    }

    @ResponseBody
    @PostMapping("/editDevice")
    public CommonResult updateDevice(@RequestBody Device device, ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        String branchId = JWTUtil.getBranchId(token);
        device.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        device.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        return faceService.updateDevice(device);
    }

    @ResponseBody
    @PostMapping("/sign")
    public String sign(PersonVO personVO) {
        return faceService.sign(personVO);
    }

    @ResponseBody
    @PostMapping("/addPerson")
    public CommonResult addPerson(@RequestBody PersonVO personVO, ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        String branchId = JWTUtil.getBranchId(token);
        personVO.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        personVO.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        return faceService.addPerson(personVO);
    }

    @ResponseBody
    @PostMapping("/addMorePerson")
    CommonResult addMorePerson(ServletRequest request, BigInteger branchId) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        return faceService.addMorePerson(BigInteger.valueOf(Long.parseLong(organizationId)), branchId);
    }

    @ResponseBody
    @PostMapping("/getNoImport")
    public CommonResult getNoImport(ServletRequest request, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        String branchId = JWTUtil.getBranchId(token);
        return faceService.getNoImport(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchId)), pageNum, pageSize);
    }
    @ResponseBody
    @PostMapping("/importStuInfo")
    public CommonResult importStuInfo(ServletRequest request){
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id,分店id
        String organizationId = JWTUtil.getOrganizationId(token);
        String branchId = JWTUtil.getBranchId(token);
        return faceService.importStuInfo(BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchId)));

    }

//    @ResponseBody
//    @PostMapping("/delPerson")
//    public CommonResult delPerson(HostInfoDto hostInfo, String deviceKey, String secret, String personId) {
//
//    }

    /*@ResponseBody
    @PostMapping("/addPersonPasstime")
    public CommonResult addPersonPasstime(HostInfoDto hostInfo, String deviceKey, String secret, PersonPasstimeDto personPasstimeDto) {

    }

    @ResponseBody
    @PostMapping("/addFace")
    public CommonResult addFace(HostInfoDto hostInfo, String deviceKey, String secret, FaceDto faceDto) {

    }

    @ResponseBody
    @PostMapping("/delFace")
    public CommonResult delFace(HostInfoDto hostInfo, String deviceKey, String secret, String faceId) {

    }*/


}
